package com.ruoyi.project.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.pay.AlipayUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.project.common.domain.Trade;
import com.ruoyi.project.system.mapper.SysAliPayMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysAliPay;
import com.ruoyi.project.system.service.ISysAliPayService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * aliPayController
 *
 * @author hongwj
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/system/aliPay")
public class SysAliPayController extends BaseController {
    @Autowired
    private ISysAliPayService sysAliPayService;

    @Autowired
    private AlipayUtils alipayUtils;

    @Autowired
    private SysAliPayMapper aliPayMapper;

    /**
     * 查询aliPay列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAliPay sysAliPay) {
        startPage();
        List<SysAliPay> list = sysAliPayService.selectSysAliPayList(sysAliPay);
        return getDataTable(list);
    }

    /**
     * 导出aliPay列表
     */
    @Log(title = "aliPay", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysAliPay sysAliPay) {
        List<SysAliPay> list = sysAliPayService.selectSysAliPayList(sysAliPay);
        ExcelUtil<SysAliPay> util = new ExcelUtil<SysAliPay>(SysAliPay.class);
        util.exportExcel(response, list, "aliPay数据");
    }

    /**
     * 获取aliPay详细信息
     */
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId) {
        return success(sysAliPayService.selectSysAliPayByConfigId(configId));
    }

    /**
     * 新增aliPay
     */
    @Log(title = "aliPay", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAliPay sysAliPay) {
        return toAjax(sysAliPayService.insertSysAliPay(sysAliPay));
    }

    /**
     * 修改aliPay
     */
    @Log(title = "aliPay", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAliPay sysAliPay) {
        return toAjax(sysAliPayService.updateSysAliPay(sysAliPay));
    }

    /**
     * 删除aliPay
     */
    @Log(title = "aliPay", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(sysAliPayService.deleteSysAliPayByConfigIds(configIds));
    }

    /**
     * 获取aliPay详细信息
     */
    @GetMapping(value = "/selectMainConfig")
    public AjaxResult selectMainConfig() {
        SysAliPay aliPay = aliPayMapper.selectMainConfig();
        if (aliPay == null) {
            return success(new SysAliPay());
        }
        return success(aliPay);
    }


    @Log(title = "支付宝支付-手机网页支付", businessType = BusinessType.OTHER)
    @ApiOperation("手机网页支付")
    @Anonymous
    @PostMapping(value = "/toPayAsWeb")
    public AjaxResult toPayAsWeb(@Validated @RequestBody Trade trade) throws Exception {
        trade.setOutTradeNo(alipayUtils.getOrderCode());
        String payUrl = sysAliPayService.toPayAsWeb(trade);
        return AjaxResult.success("success", payUrl);
    }
}
